const Sharerule = require('../model/sharerule');
const Sharedetail = require('../model/sharedetail')
const dateFormat = require('dateformat');
const indexService = require('../service/indexService');

/**
 * 插入数据
 * @param {string} params.mixnick 混淆昵称
 * @param {string} params.nickname 用户昵称
 * @param {string} params.shared_mixnick 被分享人混淆昵称
 * @param {string} params.shared_nickname 被分享人昵称
 * @param {string} params.shared_img 被分享人头像
 * @param {object} params.isVip 会员信息
 */
exports.insertOne = async function (params) {
    try {
        //查询当前时间生效的规则信息
        let ruleRes = await Sharerule.findRuleCurrentAndStartAndEnd();
        if (!ruleRes || ruleRes.length == 0) {
            return { success: false, code: 10001, msg: "分享规则不存在" };
        }
        //查看是否数据存在
        let shareCount = await Sharedetail.findCountByShareAndAccept({ mixnick: params.mixnick, shared_mixnick: params.shared_mixnick, rule_id: ruleRes.id});
        if (shareCount <= 0) {
            let vipstate = 0;
            console.log(33333)
            // //查找被分享人的会员状态，vipstate,0,不是会员，1，老会员，2，新会员
            // let isVip= await indexService.getMember(params);
            // let isVip=params.isVip;
            // if (isVip.bind_status) {
            //      if (isVip.bind_status == "1") {
            //         vipstate = 1;
            //     }
            // }
            vipstate = 2;

            params.vipstate=vipstate;
            params.rule_id=ruleRes.id;
            let data = await Sharedetail.createOne(params);
            if (data.affectedRows == 1) {
                return { success: true, code: 10000, msg: data };
            } else {
                return { success: false, code: 10002, msg: "新增数据失败" };
            }

        } else {
            return { success: false, code: 10001, msg: "数据已存在" };
        }
    }
    catch (e) {
        return { success: false, code: 10010, msg: e };
    }
}


/**
 * 用户分享明细列表
 * @param {string} params.mixnick 混淆昵称
 */
exports.shareList = async function (params) {
    try {
        //查询当前时间生效的规则信息
        let ruleRes = await Sharerule.findRuleCurrentAndStartAndEnd();
        if (!ruleRes || ruleRes.length == 0) {
            return { success: false, code: 10001, msg: "分享规则不存在" };
        }
        //查询列表
        let shareList = await Sharedetail.findAllByShare({ mixnick: params.mixnick, rule_id: ruleRes.id });
        let arr = [];
        for (let i = 0; i < shareList.length; i++) {
            if (shareList[i].shared_mixnick == params.mixnick && shareList[i].vip == 2) {
                shareList[i].type = 2;//type=2,我被邀请，成功入会
                arr.push(shareList[i]);
            } else if (shareList[i].shared_mixnick == params.mixnick && [0, 1].includes(shareList[i].vipstate)) {
                // shareList.splice(i,1);
            } else if (shareList[i].mixnick == params.mixnick) {
                shareList[i].type = 1;//type=1,我邀请别人
                arr.push(shareList[i]);
            }
        }

        return { success: true, code: 10000, msg: arr };
    } catch (e) {
        return { success: false, code: 10010, msg: e };
    }
}

/**
 * 分享成功的用户列表
 * @param {用户混淆昵称} params.mixnick
 */
exports.shareSuccessList = async function (params) {
    try {
        //查询当前时间生效的规则信息
        let ruleRes = await Sharerule.findRuleCurrentAndStartAndEnd();
        if (!ruleRes || ruleRes.length == 0) {
            return { success: false, code: 10001, msg: "分享规则不存在" };
        }

        //查询列表
        let shareList = await Sharedetail.findAllByShareSuccess({ mixnick: params.mixnick, vipstate: ruleRes.state, rule_id: ruleRes.id });

        return { success: true, code: 10000, msg: shareList };
    } catch (e) {
        return { success: false, code: 10010, msg: e };
    }
}

/**
 * 更新被分享人的会员状态
 * @param {string} params.mixnick 分享人mixnick
 * @param {string} params.shared_mixnick 被分享人mixnick
 * @param {string} params.shared_nickname 被分享人昵称
 * @param {number} params.rule_id  规则id
 * @param {object} params.isVip 会员信息
 */
exports.updateShareVip = async function (params) {
    try {
        //是否新会员
        let vipstate = 1; 
        
        //查询被分享人的会员状态，vipstate,0,不是会员，1，老会员，2，新会员
        // let isVip = params.isVip;
        //  if (isVip.bind_status) {
        //     if (isVip.bind_status == "2") {
        //         vipstate = 2;
        //     }
        // } else {
        //     vipstate = 0;
        // }

        //修改会员状态
        let updateShare = await Sharedetail.updateIsNewVipByShareMixnickAndAcceptMixnick({mixnick:params.mixnick, shared_mixnick:params.shared_mixnick, vipstate:vipstate, rule_id:params.rule_id});
        if (updateShare.affectedRows == 1) {
            return { success: true, code: 10000, msg: "修改成功" };
        } else {
            return  { success: false, code: 10001, msg: "修改失败" };
        }

    } catch (e) {
        console.log(e)
        return { success: false, code: 10010, msg: e };
    }
}

